package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.monitoring.j_monitoring.entity.EquipmentData;
import org.jeecg.modules.monitoring.j_monitoring.service.IEquipmentDataService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 设备列表(物资)
 * @Author: xiaoyq
 * @Date:   2023-04-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags="设备列表(物资)")
@RestController
@RequestMapping("/equipmentData/equipmentData")
public class EquipmentDataController extends JeecgController<EquipmentData, IEquipmentDataService> {
	@Autowired
	private IEquipmentDataService equipmentDataService;

	/**
	 * 分页列表查询
	 *
	 * @param equipmentData
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-分页列表查询")
	@ApiOperation(value="设备列表(物资)-分页列表查询", notes="设备列表(物资)-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(EquipmentData equipmentData,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<EquipmentData> queryWrapper = QueryGenerator.initQueryWrapper(equipmentData, req.getParameterMap());
		queryWrapper.apply("id in ( select max(id) from v_equipmentinfo GROUP BY code )");
		if (!StringUtils.isEmpty(equipmentData.getName())){
			queryWrapper.and(wrapper -> wrapper.like("devicestypename",equipmentData.getName())
					.or()
					.like("model",equipmentData.getName())
					.or()
					.like("code",equipmentData.getName())
					.or()
					.like("serialnumber",equipmentData.getName())
					.or()
					.like("username",equipmentData.getName())
					.or()
					.like("occupydeptname",equipmentData.getName()));
		}
		queryWrapper.select();
		Page<EquipmentData> page = new Page<EquipmentData>(pageNo, pageSize);
		IPage<EquipmentData> pageList = equipmentDataService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param equipmentData
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-添加")
	@ApiOperation(value="设备列表(物资)-添加", notes="设备列表(物资)-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody EquipmentData equipmentData) {
		equipmentDataService.save(equipmentData);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param equipmentData
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-编辑")
	@ApiOperation(value="设备列表(物资)-编辑", notes="设备列表(物资)-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody EquipmentData equipmentData) {
		equipmentDataService.updateById(equipmentData);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-通过id删除")
	@ApiOperation(value="设备列表(物资)-通过id删除", notes="设备列表(物资)-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		equipmentDataService.removeById(id);
		return Result.OK("删除成功!");
	}



	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-批量删除")
	@ApiOperation(value="设备列表(物资)-批量删除", notes="设备列表(物资)-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.equipmentDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备列表(物资)-通过id查询")
	@ApiOperation(value="设备列表(物资)-通过id查询", notes="设备列表(物资)-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		EquipmentData equipmentData = equipmentDataService.getById(id);
		return Result.OK(equipmentData);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param equipmentData
   */
  @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
  @ApiOperation(value="设备列表(物资)-导出", notes="设备列表(物资)-导出")
  public ModelAndView exportXls(HttpServletRequest request, EquipmentData equipmentData) {
      return super.exportXls(request, equipmentData, EquipmentData.class, "设备列表(物资)");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @ApiOperation(value="设备列表(物资)-导入", notes="设备列表(物资)-导入")
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, EquipmentData.class);
  }

  @RequestMapping(value = "/syncData", method = RequestMethod.GET)
  public void syncData(){
	  equipmentDataService.syncData();
  }


}
